#include "player.h"
#include "mudnmuck.h"
#include "room.h"

Player::Player(std::string name, Platinum::Net::TCP::Connection* conn)
{
   Conn = conn;
   CommandMap["NW"] = 0;
   CommandMap["N"] = 1;
   CommandMap["NE"] = 2;
   CommandMap["W"] = 3;
   CommandMap[""] = 4;
   CommandMap["E"] = 5;
   CommandMap["SW"] = 6;
   CommandMap["S"] = 7;
   CommandMap["SE"] = 8;
   CommandMap["/gsay"] = 9;
   CommandMap["/yell"] = 9;
   CommandMap["/shout"] = 9;
   CommandMap["/mv"] = 10;
   CommandMap["/move"] = 10;
   CommandMap["/go"] = 10;
   CommandMap["/stop"] = 11;
   CommandMap["/loc"] = 12;
}

void Player::AddToChatQ(std::string msg)
{
   ChatQ.push(msg);
}

void Player::SendChat()
{
   while(!ChatQ.empty())
   {
        Platinum::Net::TCP::Send(Conn,ChatQ.front());
        ChatQ.pop();
   }
}

void Player::ProcessMoveCommand(std::string OrgCmd)
{
   std::string direction = toupperStr(getFirstWord(OrgCmd));
   
   std::string distword = toupperStr(getRestWords(OrgCmd));
   int iDirection = 4;
   if(distword == "")
        distword = "1";
   
   int distance = atoi(distword.c_str());

  Cell* thisCell = CurrentCell.getAdj(CommandMap[direction]);
  std::string msg;
  for(int i = 0; i < distance; i++)
  {
        NextCell = thisCell->getAdj(CommandMap[direction]);
        if(NextCell == thisCell || NextCell->isSolid())
        {
             
             msg = "You can only move ";
             msg += i;
             msg += " paces in that direction, before you hit the wall.\n";
             Platinum::Net::TCP::Send(Conn,msg);
             break;
        }                    
        MoveQ.push(NextCell);
        msg = "You begin to move ";
        msg += distance;
        msg = " paces to the ";
        msg += direction;
        msg += "\nType /stop to stop movement at anytime";
        AddToChatQ(msg);
  }
                      

}

void Player::ProcessCommand(std::string msg)
{
   std::string outmsg;
   int command = CommandMap[getFirstWord(msg)];
   switch(command)
   {
		case 9:
             outmsg = Name;
             outmsg += " screams, ";
             outmsg += getRestWords(msg);
             //CurrentRoom->MyDungeon()->GlobalChat(outmsg);
        break;

		case 10:
              ProcessMoveCommand(getRestWords(msg));
        break;

		case 11:
                  StopMovement();
                  outmsg = "You stop moving.\n";
        break;
        
		case 12:
                  outmsg = "Your location is ";
                  outmsg += CurrentCell.getX();
                  outmsg += ",";
                  outmsg += CurrentCell.getY();
                  outmsg += "\nYou are in the ";
                  outmsg += CurrentRoom->getName();
                  outmsg += "\n";
        break;

        AddToChatQ(outmsg);
   }
}

void Player::ProcessTick()
{
	inmsg = Platinum::Net::TCP::Recv(Conn);
   if(inmsg[0] == '/')
   {
        ProcessCommand(inmsg);
   }else{            
        outmsg = "name";
        outmsg += ": ";
        outmsg += inmsg;      
        CurrentRoom->AddChat(outmsg);
   }
   //ProcessMovement();
   SendChat();
}

void Player::StopMovement()
{
	while(!MoveQ.empty())
	{
		MoveQ.pop();
	}
}